using System.Windows.Forms;
using WindowsLive.Writer.Api;

namespace WordpressCodePlugin
{
    [WriterPlugin("b0ad6aa8-17a2-487e-be61-3d3b25603fab", "Wordpress Code Plugin",
        PublisherUrl = "http://dev.scottisafool.co.uk",
        Description = "Lets you insert your code using the wordpress sourcecode tag",
        ImagePath = "writer.png",
        HasEditableOptions = true)]
    [InsertableContentSource("Wordpress Code")]

    public class Plugin : SmartContentSource
    {
        private PluginSettings m_settings;
        public override void Initialize(IProperties pluginOptions)
        {
            base.Initialize(pluginOptions);
            m_settings = new PluginSettings(pluginOptions);
        }

        public override DialogResult CreateContent(IWin32Window dialogOwner, ISmartContent newContent)
        {
            PluginSettings settings = new PluginSettings(newContent.Properties);
            using (frmMain main = new frmMain(m_settings))
            {
                DialogResult result = main.ShowDialog();
                if (result == DialogResult.OK)
                {
                    settings.Language = main.cmbLanguage.SelectedItem.ToString();
                    settings.AutoLinks = main.chbxAutoLinks.Checked;
                    settings.Collapse = main.chbxCollapse.Checked;
                    settings.FirstLine = main.chbxCollapse.Checked ? main.txbxFirstLine.Text : "";
                    settings.Gutter = main.chbxGutter.Checked;
                    settings.Highlight = main.chbxHighlight.Checked ? main.txbxHighlight.Text : "";
                    settings.HTMLScript = main.chbxHTMLScript.Checked;
                    settings.Light = main.chbxLight.Checked;
                    settings.PadLineNumbers = main.chbxPadLineNumbers.Checked;
                    settings.Toolbars = main.chbxToolbar.Checked;
                    settings.WrapLines = main.chbxWrapLines.Checked;
                    settings.Code = main.FormatCode();
                }
                return result;
            }            
        }

        public override void EditOptions(IWin32Window dialogOwner)
        {
            using (frmOptions op = new frmOptions(m_settings))
            {
                op.ShowDialog();
            }
        }

        public override SmartContentEditor CreateEditor(ISmartContentEditorSite editorSite)
        {            
            return new contentEditor();
        }

        public override string GenerateEditorHtml(ISmartContent content, IPublishingContext publishingContext)
        {
            PluginSettings settings = new PluginSettings(content.Properties);
            return string.Format("<pre>{0}</pre>", CleanMarkup(settings.Code));
        }

        private string CleanMarkup(string code)
        {
            string result = code.Replace("\n\r", "");
            result = result.Replace("<", "&lt;");
            result = result.Replace(">", "&gt;");
            return result;
        }

        public override string GeneratePublishHtml(ISmartContent content, IPublishingContext publishingContext)
        {
            PluginSettings settings = new PluginSettings(content.Properties);
            string contentString = FetchCode(settings);
            return contentString;
        }

        private string FetchCode(PluginSettings settings)
        {
            string result = string.Empty;
            result += "<pre>[sourcecode language=\"";
            result += settings.Language;
            if (!settings.AutoLinks)
                result += "\" autolinks=\"false";
            if (settings.Collapse)
                result += "\" collapse=\"true";
            if (settings.FirstLine.Length > 0)
                result += string.Format("\" firstline=\"{0}", settings.FirstLine);
            if (!settings.Gutter)
                result += "\" gutter=\"false";
            if (settings.Highlight.Length > 0)
                result += string.Format("\" highlight=\"{0}", settings.Highlight);
            if (settings.HTMLScript)
                result += "\" htmlscript=\"true";
            if (settings.Light)
                result += "\" light=\"true";
            result += string.Format("\" padlinenumbers=\"{0}", settings.PadLineNumbers);
            if (!settings.Toolbars)
                result += "\" toolbar=\"false";
            result += string.Format("\" wraplines=\"{0}", settings.WrapLines);
            result += "\"]";
            result += settings.Code;
            result += "[/sourcecode]</pre>";

            return result;
        }          
    }
}
